﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ToaThuoc_PAK
{
    public partial class DanhSachBenhNhanCho : System.Web.UI.Page
    {
        private DAL_BenhNhan dal_BenhNhan = null;
        private string _MaKhoa = "",
                        _MaBN = "",
                        _HoTen = "",
                        _TuNgay = "", _DenNgay = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["mand"] == null)
            {
                Response.Redirect("DangNhap.aspx");
                return;
            }
            dal_BenhNhan = new DAL_BenhNhan();
            _MaKhoa = Session["makhoa"].ToString();
            if (!IsPostBack)
            {
                txtTuNgay.Value = txtDenNgay.Value = _TuNgay = _DenNgay = DateTime.Now.ToString("dd/MM/yyyy");
                BindingDanhSachBenhNhanCho();
            }
        }

        private void BindingDanhSachBenhNhanCho()
        {
            List<DS_ChoKham> lstDSBenhNhanCho = dal_BenhNhan.ViewDanhSachChoKham(_MaKhoa, _MaBN, _HoTen, _TuNgay, _DenNgay, chkChoDonThuoc.Checked);
            Session["DSBenhNhanCho"] = lstDSBenhNhanCho;
            gridView.DataSource = lstDSBenhNhanCho;
            gridView.DataBind();
        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            ragViewType.SelectedIndex = -1;
            _MaBN = Factory.emptyNull(Request.Form["txtmabenhnhan"]);
            _HoTen = Factory.emptyNull(Request.Form["txttenbenhnhan"]);
            if (Factory.emptyNull(Request.Form["txtTuNgay"]) != "" || Factory.emptyNull(Request.Form["txtDenNgay"]) != "")
            {
                _TuNgay = Request.Form["txtTuNgay"];
                _DenNgay = Request.Form["txtDenNgay"];
            }
            BindingDanhSachBenhNhanCho();
        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void gridView_DataBound(object sender, EventArgs e)
        {
            string dt = "";
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                dt = gridView.Rows[i].Cells[10].Text.Trim();
                if (dt == "BHYT")
                {
                    gridView.Rows[i].CssClass = "row-bh";
                }
                if (dt == "TE")
                {
                    gridView.Rows[i].CssClass = "row-te";
                }
                string keto = gridView.Rows[i].Cells[9].Text.Trim();
                if (keto == "False") { gridView.Rows[i].ForeColor = Color.DarkMagenta; }
                gridView.Rows[i].Attributes.Add("onmouseover", "this.style.backgroundColor=\"#b0c4de\";");
                gridView.Rows[i].Attributes.Add("onmouseout", "this.style.backgroundColor=\"#ffffff\"");
                for (int k = 0; k < gridView.Rows[i].Cells.Count; k++)
                {
                    gridView.Rows[i].Cells[k].Attributes.Add("onmouseout", "this.style.cursor=\"\";");
                    gridView.Rows[i].Cells[k].Attributes.Add("onmouseover", "this.style.cursor=\"hand\";");
                    gridView.Rows[i].Cells[k].Attributes.Add("onclick", " showdsdangky('" + gridView.Rows[i].Cells[0].Text.Trim() + "','" + gridView.Rows[i].Cells[3].Text.Trim() + "', '" + i + "')");
                }
              
            }
        }

        protected void gridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            //if (Session["DSBenhNhanCho"] != null)
            //{
            //    List<DSBenhNhanCho> lst = Session["DSBenhNhanCho"] as List<DSBenhNhanCho>;
            //    DSBenhNhanCho BenhNhan = lst[e.NewSelectedIndex];
            //    Session["ThongTinBenhNhan"] = BenhNhan;
            //    Response.Write("ToaThuoc.aspx?s_idthu=" + BenhNhan.IDCT + "&s_mabn=" + BenhNhan.MaBN + "");
            //}
        }
        
        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindingDanhSachBenhNhanCho();
        }

        protected void ragViewType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string value = ragViewType.SelectedValue;
            switch (value)
            {
                case "TrongNgay":
                    _TuNgay = DateTime.Now.ToShortDateString() + " 00:00";
                    _DenNgay = DateTime.Now.ToShortDateString() + " 23:59";
                    break;
                case "TatCa":
                    txtTuNgay.Value = txtDenNgay.Value = "";
                    break;
            }
            BindingDanhSachBenhNhanCho();
        }
    }
}